package com.woniuxy.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.woniuxy.jsonserial.CustomTimeSerializer;
import com.woniuxy.jsonserial.DateDeserializer;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Data
@Document(collection = "file_record")
public class FileRecord {
    @Id
    private String id;

    @Field(name = "file_name")
    private String fileName;

    @Field(name = "file_content")
    @JsonIgnore
    private byte[] fileContent;

    @Field(name = "file_content_type")
    private String fileContentType;

    @Field(name = "create_time")
    @JsonSerialize(using = CustomTimeSerializer.class)
    @JsonDeserialize(using = DateDeserializer.class)
    private Date createTime;

    @Field(name = "creator")
    private String creator;

    @Field(name = "del_flag")
    private Boolean delFlag;

    public void setFile(final MultipartFile file) throws IOException {
        this.createTime = new Date();
        this.fileContent = file.getBytes();
        this.fileContentType = file.getContentType();
        this.fileName = file.getOriginalFilename();
        this.delFlag = false;
    }
}
